import java.util.Arrays;

class Solution {
    public int findRoot(int[] elem, int x) {
        while(elem[x] >= 0) {
            x = elem[x];
        }
        return x;
    }

    public void union(int[] elem, int x1, int x2) {
        if(findRoot(elem,x1) == findRoot(elem,x2)) {
            return;
        }
        int r1 = findRoot(elem,x1);
        int r2 = findRoot(elem,x2);
        elem[r1] += elem[r2];
        elem[r2] = x1;
    }

    public boolean equationsPossible(String[] equations) {
        int[] elem = new int[26];
        Arrays.fill(elem,-1);
        int n = equations.length;
        for(int i = 0; i < n; i++) {
            if(equations[i].charAt(1) == '=') {
                union(elem, equations[i].charAt(0) - 'a', equations[i].charAt(3) - 'a');
            }
        }

        for(int i = 0; i < n; i++) {
            if(equations[i].charAt(1) == '!') {
                int r1 = findRoot(elem,equations[i].charAt(0) - 'a');
                int r2 = findRoot(elem,equations[i].charAt(3) - 'a');
                if(r1 == r2) {
                    return false;
                }
            }
        }
        return true;
    }
}